// license-header java merge-point
/**
 * This is only generated once! It will never be overwritten.
 * You can (and have to!) safely modify it by hand.
 */
package fr.upstlse.glre.business.domain.user;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.hibernate.Criteria;
import org.hibernate.criterion.Restrictions;

/**
 * @see fr.upstlse.glre.business.domain.user.Etudiant
 */
public class EtudiantDaoImpl
    extends fr.upstlse.glre.business.domain.user.EtudiantDaoBase
{
	static Logger LOGGER = Logger.getLogger(EtudiantDaoImpl.class);
    /**
     * @see fr.upstlse.glre.business.domain.user.EtudiantDao#findByCriteria(fr.upstlse.glre.business.vo.user.EtudiantSearchCriteriaVO)
     */
    protected java.util.List handleFindByCriteria(fr.upstlse.glre.business.vo.user.EtudiantSearchCriteriaVO pEtudiantSearchCriteriaVO)
    {
    	//Create EtudiantCriteria
		Criteria lEtudiantCriteria = getSession().createCriteria(Etudiant.class);
		
		if(pEtudiantSearchCriteriaVO.getLogin() != null) {
			if(StringUtils.contains(pEtudiantSearchCriteriaVO.getLogin(), "%")) {
				lEtudiantCriteria.add(Restrictions.like("login", pEtudiantSearchCriteriaVO.getLogin()));
			}
			else {
				lEtudiantCriteria.add(Restrictions.eq("login", pEtudiantSearchCriteriaVO.getLogin()));	
			}
		}
		
		if(pEtudiantSearchCriteriaVO.getPassword() != null) {
			if(StringUtils.contains(pEtudiantSearchCriteriaVO.getPassword(), "%")) {
				lEtudiantCriteria.add(Restrictions.like("password", pEtudiantSearchCriteriaVO.getPassword()));
			}
			else {
				lEtudiantCriteria.add(Restrictions.eq("password", pEtudiantSearchCriteriaVO.getPassword()));	
			}
		}
		
		if(pEtudiantSearchCriteriaVO.getRole() != null) {
			if(StringUtils.contains(pEtudiantSearchCriteriaVO.getRole().getValue(), "%")) {
				lEtudiantCriteria.add(Restrictions.like("role", pEtudiantSearchCriteriaVO.getRole()));
			}
			else {
				lEtudiantCriteria.add(Restrictions.eq("role", pEtudiantSearchCriteriaVO.getRole()));	
			}
		}
		
		if(pEtudiantSearchCriteriaVO.getNom() != null) {
			if(StringUtils.contains(pEtudiantSearchCriteriaVO.getNom(), "%")) {
				lEtudiantCriteria.add(Restrictions.like("nom", pEtudiantSearchCriteriaVO.getNom()));
			}
			else {
				lEtudiantCriteria.add(Restrictions.eq("nom", pEtudiantSearchCriteriaVO.getNom()));	
			}
		}
		
		if(pEtudiantSearchCriteriaVO.getPrenom() != null) {
			if(StringUtils.contains(pEtudiantSearchCriteriaVO.getPrenom(), "%")) {
				lEtudiantCriteria.add(Restrictions.like("prenom", pEtudiantSearchCriteriaVO.getPrenom()));
			}
			else {
				lEtudiantCriteria.add(Restrictions.eq("prenom", pEtudiantSearchCriteriaVO.getPrenom()));	
			}
		}
		
		if(pEtudiantSearchCriteriaVO.getEmail() != null) {
			if(StringUtils.contains(pEtudiantSearchCriteriaVO.getEmail(), "%")) {
				lEtudiantCriteria.add(Restrictions.like("email", pEtudiantSearchCriteriaVO.getEmail()));
			}
			else {
				lEtudiantCriteria.add(Restrictions.eq("email", pEtudiantSearchCriteriaVO.getEmail()));	
			}
		}
		return lEtudiantCriteria.list();
}

    /**
     * @see fr.upstlse.glre.business.domain.user.EtudiantDao#toEtudiantVO(fr.upstlse.glre.business.domain.user.Etudiant, fr.upstlse.glre.business.vo.user.EtudiantVO)
     */
    public void toEtudiantVO(
        fr.upstlse.glre.business.domain.user.Etudiant source,
        fr.upstlse.glre.business.vo.user.EtudiantVO target)
    {
    	super.toEtudiantVO(source, target);
    	target.setAdresseVO(getAdresseDao().toAdresseVO(source.getAdresse()));
    	if(source.getStage() != null) {
    		target.setStageVO(getStageDao().toStageVO(source.getStage()));	
    	}
    	
    }


    /**
     * @see fr.upstlse.glre.business.domain.user.EtudiantDao#toEtudiantVO(fr.upstlse.glre.business.domain.user.Etudiant)
     */
    public fr.upstlse.glre.business.vo.user.EtudiantVO toEtudiantVO(final fr.upstlse.glre.business.domain.user.Etudiant entity)
    {
        return super.toEtudiantVO(entity);
    }


    /**
     * Retrieves the entity object that is associated with the specified value object
     * from the object store. If no such entity object exists in the object store,
     * a new, blank entity is created
     */
    private fr.upstlse.glre.business.domain.user.Etudiant loadEtudiantFromEtudiantVO(fr.upstlse.glre.business.vo.user.EtudiantVO etudiantVO)
    {
        fr.upstlse.glre.business.domain.user.Etudiant etudiant = (Etudiant) this.load(etudiantVO.getId());
        if (etudiant == null)
        {
            etudiant = fr.upstlse.glre.business.domain.user.Etudiant.Factory.newInstance();
        }
        return etudiant;
        
    }

    
    /**
     * @see fr.upstlse.glre.business.domain.user.EtudiantDao#etudiantVOToEntity(fr.upstlse.glre.business.vo.user.EtudiantVO)
     */
    public fr.upstlse.glre.business.domain.user.Etudiant etudiantVOToEntity(fr.upstlse.glre.business.vo.user.EtudiantVO etudiantVO)
    {
        fr.upstlse.glre.business.domain.user.Etudiant entity = this.loadEtudiantFromEtudiantVO(etudiantVO);
        this.etudiantVOToEntity(etudiantVO, entity, true);
        return entity;
    }


    /**
     * @see fr.upstlse.glre.business.domain.user.EtudiantDao#etudiantVOToEntity(fr.upstlse.glre.business.vo.user.EtudiantVO, fr.upstlse.glre.business.domain.user.Etudiant)
     */
    public void etudiantVOToEntity(
			fr.upstlse.glre.business.vo.user.EtudiantVO source,
			fr.upstlse.glre.business.domain.user.Etudiant target,
			boolean copyIfNull) {
		super.etudiantVOToEntity(source, target, copyIfNull);
		if (copyIfNull || source.getAdresseVO() != null) {
			target.setAdresse(getAdresseDao().adresseVOToEntity(source.getAdresseVO()));
		}
		if (copyIfNull || source.getStageVO() != null) {
			target.setStage(getStageDao().stageVOToEntity(source.getStageVO()));
		}
	}
    
    public java.lang.Object create(final int transform, final fr.upstlse.glre.business.domain.user.Etudiant etudiant)
    {
        if (etudiant == null)
        {
            throw new IllegalArgumentException(
                "Etudiant.create - 'etudiant' can not be null");
        }
        getAdresseDao().create(etudiant.getAdresse());
        if(etudiant.getStage() != null) {
        	getStageDao().create(etudiant.getStage());
        }
        this.getHibernateTemplate().save(etudiant);
        return this.transformEntity(transform, etudiant);
    }
    
    /**
     * @see fr.upstlse.glre.business.domain.user.EtudiantDao#update(fr.upstlse.glre.business.domain.user.Etudiant)
     */
    public void update(fr.upstlse.glre.business.domain.user.Etudiant etudiant)
    {
        if (etudiant == null)
        {
            throw new IllegalArgumentException(
                "Etudiant.update - 'etudiant' can not be null");
        }
        
        getAdresseDao().update(etudiant.getAdresse());
        if(etudiant.getStage() != null) {
        	getStageDao().update(etudiant.getStage());
        }
        this.getHibernateTemplate().update(etudiant);
    }
    
    
}